<?
require_once('unicode.php');

function print_entry($key,$entry)
{
  $line = sprintf("'%s': [",$key);
  for($i=0;$i<count($entry);$i++) {
    $str = $entry[$i];

    $line .= "[";
    for($j=0;$j<strlen($str);$j += utf8len($str[$j])) {
      $line .= sprintf("0x%x,",readutf8(substr($str,$j)));
    }
    $line = rtrim($line,',');
    $line .= "],";
  }
  $line = rtrim($line,',');
  $line .= "],";
  printf("%s\n",$line);
}
function cmp_func($a,$b)
{
  if($a < $b) return 1;
  else if($a > $b) return -1;
  else return 0;
}

if(count($argv) < 2) return;

$fp = fopen($argv[1],'r');

$entries = array();
$pop_array = array();
while($line = fgets($fp)) {
  $line = rtrim($line,"\n");
  list($txt,$data,$pop) = split(' ',$line);

  $pop_array[$data] = (int)$pop;

  if(key_exists($txt,$entries)) {
    $entry = $entries[$txt];
    $entry[] = $data;
    $entries[$txt] = $entry;
  }
  else {
    $entry = array();
    $entry[] = $data;
    $entries[$txt] = $entry;
  }
}
fclose($fp);

// sort and make the pop_array index into successive
uasort($pop_array,cmp_func);
$c = 0;
foreach($pop_array as $key => $data) {
  $pop_array[$key] = $c++;
}

printf("table: {\n");
$count = 0;
// print first 15000 words
foreach($entries as $key => $entry) {
  $ent = $entry[0];
  if($pop_array[$ent] <= 15000) print_entry($key,$entry);
  else print_entry($key,array());
}
printf("},\n");

// print 15001-30000 words
printf("//BEGIN_COMPONENT{table0}\n");
printf("table0: {\n");
foreach($entries as $key => $entry) {
  $ent = $entry[0];
  if(15001 <= $pop_array[$ent] && $pop_array[$ent] <= 30000) print_entry($key,$entry);
}
printf("},\n");
printf("//END_COMPONENT\n");

// print 30001-45000 words
printf("//BEGIN_COMPONENT{table1}\n");
printf("table1: {\n");
foreach($entries as $key => $entry) {
  $ent = $entry[0];
  if(30001 <= $pop_array[$ent] && $pop_array[$ent] <= 45000) print_entry($key,$entry);
}
printf("},\n");
printf("//END_COMPONENT\n");

// print 45001-60000 words
printf("//BEGIN_COMPONENT{table2}\n");
printf("table2: {\n");
foreach($entries as $key => $entry) {
  $ent = $entry[0];
  if(45001 <= $pop_array[$ent] && $pop_array[$ent] <= 60000) print_entry($key,$entry);
}
printf("},\n");
printf("//END_COMPONENT\n");

// print remaining
printf("//BEGIN_COMPONENT{table3}\n");
printf("table3: {\n");
foreach($entries as $key => $entry) {
  $ent = $entry[0];
  if(60001 <= $pop_array[$ent]) print_entry($key,$entry);
}
printf("},\n");
printf("//END_COMPONENT\n");


?>
